package com.terrynoya.fight.stateMachine
{
	import com.terrynoya.coreLib.MHashMap;
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.char.Entity;
	import com.terrynoya.fight.evaluation.Expression;
	import com.terrynoya.fight.stateMachine.triggers.ITrigger;

	public class TriggerMap
	{
		private var _triggers:MHashMap;
		
		public function TriggerMap()
		{
			this._triggers = new MHashMap();	
		}
		
		public function add(id:int,value:ITrigger):void
		{
			if(!this._triggers.containsKey(id))
			{
				this._triggers.put(id,new Array());
			}
			var list:Array = this._triggers.getValue(id);
			list.push(value);
		}

		public function Trigger(character:Entity):Boolean
		{
			var len:int = this._triggers.length;
			for (var i:int = 0; i < len; i++) 
			{
				var key:String = this._triggers.keys[i];
				var list:Array = this._triggers.getValue(key);
				var ok:Boolean = true;
				for (var j:int = 0; j < list.length; j++) 
				{
					var exp:Expression = list[j];
					if(exp.isValid(character) == false)
					{
						ok = false;
						break;
					}
				}
				var triggerId:int = int(key);
				if(ok == true)
				{
					if(triggerId != 0)
					{
						return true;
					}
				}
				else
				{
					if(triggerId == 0)
					{
						return false;
					}
				}
				
			}
			return false;
		}
		
		
//		public function Trigger(character:Entity):Boolean
//		{
//			var len:int = this._triggers.length;
//			for (var i:int = 0; i < len; i++) 
//			{
//				var ok:Boolean = true;
//				var trigger:ITrigger = this._triggers[i];
//				if(trigger.isValid(character) == false)
//				{
//					ok = false;
//					break;
//				}
//				if(ok == true)
//				{
//					return true;
////					if(i != 0) return false
//				}
//				else
//				{
//					if(i == 0) return false;
//				}
//			}
//			return false;
//		}
	}
}